<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\System as SystemModule;
use think\exception\ValidateException;

class System extends BaseController
{
    // 基本设置信息
    public function baseinfo() {
        try {
            $baseinfo = SystemModule::baseinfo();
        } catch(ValidateException $e) {
            return validateJson($e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($baseinfo);
    }

    public function baseseting() {
        $param = $this->request->post();
        try {
            $data = SystemModule::baseseting($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        
        return successJson($data);
    }

    // 设备列表
    public function deviceList() {
        $param = $this->request->get([
            'offset' => $this->request->get('page/d')?:0,
            'length' => $this->request->get('size/d')?:20
        ]);

        try {
            $list = SystemModule::deviceList($param);
        } catch(ValidateException $e) {
            return validateJson($e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($list);
    }

    /* 设备添加 */
    public function deviceAdd() {
        $param = $this->request->post([
            'dev_type',
            'dev_name',
            'dev_model',
            'dev_model_name',
            'dev_ip',
            'port',
            'remark'
        ]);

        if(isset($param['id'])) { // 添加
          unset($param['id']);
        }
    
        try {
          // $this->validate($param, 'app\common\validate\Material.scenecopysave');
          $data = SystemModule::deviceEdit($param);
        } catch (ValidateException $e) {
          return validateJson([], $e->getError());
        }  catch(\Exception $e) {
          return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);        
    }

    /* 设备编辑 */
    public function deviceEdit() {
        $param = $this->request->put([
            'id',
            'dev_type',
            'dev_name',
            'dev_model',
            'dev_model_name',
            'dev_ip',
            'port',
            'remark'
        ]);

        try {
            $result = SystemModule::deviceEdit($param);
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }
        return successJson($result);
    }

    /**
     * 设备删除
     */
    public function deviceDel() {
        // 接收删除的id数组
        $ids = $this->request->delete();

        try {
            $res = SystemModule::deviceDel($ids);
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($res);
    }

    /**
     * 获取当前系统版本
     */
    public function sysversion() {

        try {
            $res = SystemModule::baseinfo();
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($res);
    }
}